﻿using System.Collections.Generic;

namespace MyResKit
{
    public class ResMgr
    {
        /// <summary>
        /// 共享的 
        /// </summary>
        public static List<Res> SharedLoadedReses = new List<Res>();


        public static void Init()
        {
            AssetTable.Load();
        }
        
        public static Res GetRes(string resName, string assetBundleName)
        {
            var retRes = SharedLoadedReses.Find(loadedAsset => loadedAsset.Name == resName);

            if (retRes != null)
            {
                retRes.Retain();

                return retRes;
            }

            if (resName.StartsWith("resources://"))
            {
                retRes = new ResourcesRes(resName);
            }
            else
            {
                retRes = new AssetRes(resName, assetBundleName);
            }

            retRes.Load();

            SharedLoadedReses.Add(retRes);

            retRes.Retain();

            return retRes;
        }

        public static AssetBundleRes GetBundleRes(string assetBundleName)
        {
            var retRes = SharedLoadedReses.Find(loadedAsset => loadedAsset.Name == assetBundleName);

            if (retRes != null)
            {
                retRes.Retain();

                return retRes as AssetBundleRes;
            }

            retRes = new AssetBundleRes(assetBundleName);


            retRes.Load();

            SharedLoadedReses.Add(retRes);

            retRes.Retain();

            return retRes as AssetBundleRes;
        }
    }
}